//
//  FKMineVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/13.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKMineVC.h"
#import "FKMineCell.h"
#import "FKOrderVC.h"
#import "FKMyInfoVC.h"
#import "FKMyBalanceVC.h"
#import "FKMyCollectVC.h"
#import "FKMyCouponVC.h"

@interface FKMineVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, weak) UIImageView *avatarView;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *balanceLabel;
@property (nonatomic, weak) UILabel *couponLabel;
@property (nonatomic, weak) UILabel *collectLabel;

@property (nonatomic, copy) NSString *serviceTel;

@end

@implementation FKMineVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
        _datas = @[@{@"icon" : @"my_order", @"title" : @"核销订单", @"hideSeparator" : @(false), @"bgImg" : @"cell_up"},
                   @{@"icon" : @"my_logistics", @"title" : @"物流订单", @"hideSeparator" : @(false), @"bgImg" : @"bg_white"},
                   @{@"icon" : @"my_Invitation", @"title" : @"填写邀请码", @"hideSeparator" : @(false), @"bgImg" : @"bg_white"},
                   @{@"icon" : @"my_cart", @"title" : @"购物车", @"hideSeparator" : @(false), @"bgImg" : @"bg_white"},
                   @{@"icon" : @"my_location", @"title" : @"收货地址", @"hideSeparator" : @(false), @"bgImg" : @"bg_white"},
                   @{@"icon" : @"my_service", @"title" : @"联系客服", @"hideSeparator" : @(false), @"bgImg" : @"bg_white"},
                   @{@"icon" : @"my_feedback", @"title" : @"意见反馈", @"hideSeparator" : @(false), @"bgImg" : @"bg_white"},
                   @{@"icon" : @"my_shop", @"title" : @"商家合作", @"hideSeparator" : @(false), @"bgImg" : @"bg_white"},
                   @{@"icon" : @"my_setting", @"title" : @"设置", @"hideSeparator" : @(true), @"bgImg" : @"cell_down"}];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellStyleDefault;
    if (@available(iOS 11.0, *)) {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    // 头部
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, fk_adjustW(240) + fkOffsetTop)];
    tableView.tableHeaderView = header;
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, fk_adjustW(200) + fkOffsetTop)];
    [header addSubview:bgView];
    bgView.backgroundColor = fkMainColor;
    
    UIImageView *avatarView = [[UIImageView alloc] initWithImage:kDefaultAvatar];
    [header addSubview:avatarView];
    self.avatarView = avatarView;
    [avatarView fk_viewCornerRadius:fk_adjustW(35) borderWidth:0 borderColor:nil];
    avatarView.userInteractionEnabled = true;
    [avatarView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(enterUserCenter)]];
    
    UILabel *nameLabel = [UILabel fk_labelWithFont:fk_adjustFont(18) textColor:[UIColor whiteColor] text:@"登录/注册"];
    [header addSubview:nameLabel];
    self.nameLabel = nameLabel;
    nameLabel.userInteractionEnabled = true;
    [nameLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(enterUserCenter)]];
    
    UIView *bottomView = [[UIView alloc] init];
    [header addSubview:bottomView];
    bottomView.backgroundColor = [UIColor whiteColor];
    [bottomView fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
    
    NSArray *titles = @[@"余额", @"优惠券", @"收藏"];
    CGFloat margin = 4;
    CGFloat bottomViewW = fkScreenW - 24;
    CGFloat labelW = (bottomViewW - 4 * 4) / 3.f;
    CGFloat labelH = fk_adjustW(75);
    for (NSInteger index = 0; index < titles.count; index++) {
        NSString *text = fkFORMAT(@"--\n%@", titles[index]);
        UILabel *label = [UILabel fk_labelWithFont:fk_adjustFont(18) textColor:fkMainColor textAlignment:NSTextAlignmentCenter text:text];
        [bottomView addSubview:label];
        label.numberOfLines = 2;
        label.tag = 10 + index;
        label.attributedText = [label.text fk_addAttributeStyle:titles[index] color:fkColor666666 font:fk_adjustFont(12)];
        label.userInteractionEnabled = true;
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemLabelDidTap:)]];
        
        CGFloat labelX = margin + index * (labelW + margin);
        label.frame = CGRectMake(labelX, 0, labelW, labelH);
    }
    self.balanceLabel = [bottomView viewWithTag:10];
    self.couponLabel = [bottomView viewWithTag:11];
    self.collectLabel = [bottomView viewWithTag:12];
    
    CGFloat lineW = 1;
    CGFloat lineH = fk_adjustW(24);
    CGFloat line1X = (bottomViewW - 2) / 3.f;
    CGFloat lineY = (labelH - lineH) * 0.5;
    UIView *line1 = [UIView fk_lineWithFrame:CGRectMake(line1X, lineY, lineW, lineH)];
    [bottomView addSubview:line1];
    
    CGFloat line2X = line1X * 2 + lineW;
    UIView *line2 = [UIView fk_lineWithFrame:CGRectMake(line2X, lineY, lineW, lineH)];
    [bottomView addSubview:line2];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.bottom.offset(-fkTabBarH - fkOffsetBottom);
    }];
    [avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(fk_adjustW(44) + fkOffsetTop);
        make.width.height.offset(fk_adjustW(70));
        make.centerX.offset(0);
    }];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(avatarView.mas_bottom).offset(fk_adjustW(10));
        make.centerX.offset(0);
    }];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(fkScreenW - 24);
        make.height.offset(fk_adjustW(75));
        make.centerX.offset(0);
        make.bottom.equalTo(bgView.mas_bottom).offset(fk_adjustW(40));
    }];
    
}

- (void)itemLabelDidTap:(UITapGestureRecognizer *)gesture
{
    UILabel *label = (UILabel *)gesture.view;
    if (label.tag == 10) {
        FKMyBalanceVC *vc = [[FKMyBalanceVC alloc] init];
        fkPushNav(vc);
    } else if (label.tag == 11) {
        FKMyCouponVC *vc = [[FKMyCouponVC alloc] init];
        fkPushNav(vc);
    } else if (label.tag == 12) {
        FKMyCollectVC *vc = [[FKMyCollectVC alloc] init];
        fkPushNav(vc);
    }
}

- (void)enterUserCenter
{
    fkWeakSelf(self);
    [FKLoginTool checkLogin:^{
        FKMyInfoVC *vc = [[FKMyInfoVC alloc] init];
        [weakself.navigationController pushViewController:vc animated:true];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([FKCacheTool isLogin]) {
        [self loadData];
    } else {
        self.nameLabel.text = @"登录/注册";
        self.avatarView.image = kDefaultAvatar;
        self.balanceLabel.text = @"--\n余额";
        self.balanceLabel.attributedText = [self.balanceLabel.text fk_addAttributeStyle:@"余额" color:fkColor666666 font:fk_adjustFont(12)];
        self.couponLabel.text = @"--\n优惠券";
        self.couponLabel.attributedText = [self.couponLabel.text fk_addAttributeStyle:@"优惠券" color:fkColor666666 font:fk_adjustFont(12)];
        self.collectLabel.text = @"--\n收藏";
        self.collectLabel.attributedText = [self.collectLabel.text fk_addAttributeStyle:@"收藏" color:fkColor666666 font:fk_adjustFont(12)];
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKMineCell *cell = [FKMineCell cellWith:tableView];
    cell.data = self.datas[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(44);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = self.datas[indexPath.row];
    NSString *title = dict[@"title"];
    if ([title isEqualToString:@"核销订单"]) {
        FKOrderVC *vc = [[FKOrderVC alloc] init];
        vc.type = @"1";
        vc.title = title;
        fkPushNav(vc);
    } else if ([title isEqualToString:@"物流订单"]) {
        FKOrderVC *vc = [[FKOrderVC alloc] init];
        vc.type = @"2";
        vc.title = title;
        fkPushNav(vc);
    } else if ([title isEqualToString:@"填写邀请码"]) {
        fkPushNav([[NSClassFromString(@"FKInviteCodeVC") alloc] init]);
    } else if ([title isEqualToString:@"购物车"]) {
        fkPushNav([[NSClassFromString(@"FKShoppingCartVC") alloc] init]);
    } else if ([title isEqualToString:@"收货地址"]) {
        fkPushNav([[NSClassFromString(@"FKAddressVC") alloc] init]);
    } else if ([title isEqualToString:@"联系客服"]) {
        if (self.serviceTel) {
            fk_call(self.serviceTel);
        }
    } else if ([title isEqualToString:@"意见反馈"]) {
        fkPushNav([[NSClassFromString(@"FKFeedbackVC") alloc] init]);
    } else if ([title isEqualToString:@"商家合作"]) {
        fkPushNav([[NSClassFromString(@"FKBusinessCooperationVC") alloc] init]);
    } else if ([title isEqualToString:@"设置"]) {
        fkPushNav([[NSClassFromString(@"FKMySettingVC") alloc] init]);
    }
}

#pragma mark - 网络请求
- (void)loadData
{
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=person_info" param:nil success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            // 缓存用户信息
            NSDictionary *userInfo = result.data;
            [FKCacheTool saveUserInfo:userInfo];
            
            [self.avatarView sd_setImageWithURL:[NSURL URLWithString:userInfo[@"avatar"]] placeholderImage:kDefaultAvatar];
            self.nameLabel.text = userInfo[@"nickname"];
            self.balanceLabel.text = fkFORMAT(@"%@\n余额", userInfo[@"balance"]);
            self.balanceLabel.attributedText = [self.balanceLabel.text fk_addAttributeStyle:@"余额" color:fkColor666666 font:fk_adjustFont(12)];
            self.couponLabel.text = fkFORMAT(@"%@\n优惠券", userInfo[@"coupon_count"]);
            self.couponLabel.attributedText = [self.couponLabel.text fk_addAttributeStyle:@"优惠券" color:fkColor666666 font:fk_adjustFont(12)];
            self.collectLabel.text = fkFORMAT(@"%@\n收藏", userInfo[@"collect_count"]);
            self.collectLabel.attributedText = [self.collectLabel.text fk_addAttributeStyle:@"收藏" color:fkColor666666 font:fk_adjustFont(12)];
        }
    } failure:nil];
}

- (void)getServicePhone
{
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=kf_tel" param:nil success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            self.serviceTel = result.data[@"tel"];
        }
    } failure:nil];
}


@end
